<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\Members;
use Phalcon\Mvc\Model;


/**
 * 添加记事本会员
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class ModifyMember extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\AddInorout
     */
    private $success = 0;
    private $id = 0;

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $id = $this->getDataItem('id',0);
    	$member_name = $this->getDataItem('member_name');
    	$member_tel = $this->getDataItem('member_tel');
    	$remark = $this->getDataItem('remark');
    	
    	//验证参数是否合法
    	if (empty($member_name)){
    	    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,'会员名为空');
    	}
    	if (!empty($member_tel)&&strlen($member_tel)!=11){
    	    return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"电话号码为11位数字");
    	}
    	if($id == 0 ){
    	    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,"id不能为空");
    	}
    	
    	$info = Members::findFirst("member_id = '{$id}' AND user_id = {$this->getUserAuth()->userId}");
    	
    	if (!$info){//验证会员是否存在
    	    return $this->errorLog(ResultStatus::QUERY_DATA_NOT_EXIST,"修改的数据不存在");
    	}
    	
    	$data['member_name'] = $member_name;
    	$data['member_tel'] = $member_tel;
    	$data['remark'] = $remark;
    	
    	if ($info->update($data)){//保存用户信息
    	    $this->id = $info->member_id;
    	    $this->success = 1;
    	    
    	    $this->setResult([
    	        'success' => $this->success,
    	        'message' => '修改会员成功'
    	    ]);
    	    
    	}
    	else {
    	    $this->setResult(['success'=>0,'message'=>'修改失败！']);
    	}
        
        
    }

}